import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
    {
        path: '/login',
        component: () => import('@/views/login/index'),
        name: 'Login',
        hidden: true
    },

    {
        path: '/register',
        component: () => import('@/views/login/register'),
        name: 'Register',
        hidden: true
    },

    {
        path: '/password_reset',
        component: () => import('@/views/login/forget-pass'),
        name: 'ForgetPass',
        hidden: true
    },

    {
        path: '/404',
        component: () => import('@/views/404'),
        hidden: true
    },

    {
        path: '/profile',
        component: Layout,
        redirect: '/profile/index',
        hidden: true,
        children: [
            {
                path: 'index',
                component: () => import('@/views/profile/index'),
                name: 'Profile',
                meta: { title: '个人信息', icon: 'user', noCache: true }
            }
        ]
    }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

    // 普通入口主页
    {
        path: '/',
        component: Layout,
        redirect: '/home',
        meta: { roles: ['admin', 'editor'] },
        children: [{
            path: 'home',
            name: 'Home',
            component: () => import('@/views/home/index'),
            meta: { title: '主页', icon: 'dashboard', roles: ['admin', 'editor'] }
        }]
    },

    // 教师入口主页
    {
        path: '/',
        component: Layout,
        redirect: '/home',
        meta: { roles: ['teacher'] },
        children: [{
            path: 'home',
            name: 'Home',
            component: () => import('@/views/user/teacher-edit'),
            meta: { title: '教师信息', icon: 'el-icon-user-solid', roles: ['teacher'] }
        }]
    },

    // 管理员专用用户管理
    {
        path: '/user',
        component: Layout,
        redirect: '/user/list',
        name: 'User',
        meta: {
            title: '用户',
            icon: 'user',
            roles: ['admin']
        },
        children: [
            {
                path: 'list',
                component: () => import('@/views/user/user-list'),
                name: 'UserList',
                meta: { title: '用户列表', icon: 'list', roles: ['admin'] }
            },
            {
                path: 'verify',
                component: () => import('@/views/user/verifying-list'),
                name: 'VerifyingList',
                meta: { title: '审核列表', icon: 'el-icon-view', roles: ['admin'] }
            },
            {
                path: '/teacher/edit/:id(\\d+)',
                component: () => import('@/views/user/teacher-edit'),
                name: 'TeacherEdit',
                meta: { title: '教师信息', noCache: true, activeMenu: '/user/list', roles: ['admin'] },
                hidden: true
            }
        ]
    },

    // 大类管理
    {
        path: '/category',
        component: Layout,
        redirect: '/category/tree',
        name: 'Category',
        meta: {
            title: '大类',
            icon: 'el-icon-menu',
            roles: ['admin', 'editor']
        },
        children: [

            {
                path: 'nav',
                component: () => import('@/views/category/nav-title'),
                name: 'NavTitle',
                meta: { title: '导航栏设置', icon: 'el-icon-guide', roles: ['admin', 'editor'] }
            },
            {
                path: 'tree',
                component: () => import('@/views/category/tree/index'),
                name: 'CategoryTree',
                meta: { title: '层级结构', icon: 'tree-table', roles: ['admin', 'editor'] }
            }
        ]
    },

    // 文章管理
    {
        path: '/post',
        component: Layout,
        redirect: '/post/list',
        name: 'Post',
        meta: {
            title: '文章',
            icon: 'el-icon-document',
            roles: ['admin', 'editor']
        },
        children: [
            {
                path: 'list',
                component: () => import('@/views/post/list'),
                name: 'ArticleList',
                meta: { title: '文章列表', icon: 'list', roles: ['admin', 'editor'] }
            },
            {
                path: 'create',
                component: () => import('@/views/post/create'),
                name: 'CreateArticle',
                meta: { title: '创建文章', icon: 'edit', roles: ['admin', 'editor'] }
            },
            {
                path: 'edit/:id(\\d+)',
                component: () => import('@/views/post/edit'),
                name: 'EditArticle',
                meta: { title: '编辑文章', noCache: true, activeMenu: '/post/list', roles: ['admin', 'editor'] },
                hidden: true
            }
        ]
    },

    // 横幅管理
    {
        path: '/banner',
        component: Layout,
        redirect: '/banner/list',
        name: 'Banner',
        meta: {
            title: '横幅',
            icon: 'el-icon-picture',
            roles: ['admin', 'editor']
        },
        children: [
            {
                path: 'list',
                component: () => import('@/views/banner/list'),
                name: 'BannerList',
                meta: { title: '横幅列表', icon: 'list', roles: ['admin', 'editor'] }
            },
            {
                path: 'create',
                component: () => import('@/views/banner/create'),
                name: 'CreateBanner',
                meta: { title: '创建横幅', icon: 'edit', roles: ['admin', 'editor'] }
            },
            {
                path: 'edit/:id(\\d+)',
                component: () => import('@/views/banner/edit'),
                name: 'EditBanner',
                meta: { title: '编辑横幅', noCache: true, activeMenu: '/banner/list', roles: ['admin', 'editor'] },
                hidden: true
            }
        ]
    },

    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
    // mode: 'history', // require service support
    base: '/admin/',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
